package me.kafeitu.demo.activiti.service.oa.announce;

import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import me.kafeitu.demo.activiti.entity.oa.AnnounceJpaEntity;

import org.activiti.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公告实体JPA 实体管理器
 * @author: Henry Yan
 */
@Service
public class AnnounceEntityManager {
	@PersistenceContext
	private EntityManager entityManager;
	
	/* 把流程变量的值赋值给JPA实体对象并保存到数据库 */
	@Transactional
	public AnnounceJpaEntity newAnnounce(DelegateExecution execution){
		AnnounceJpaEntity announce = new AnnounceJpaEntity();
		announce.setProcessInstanceId(execution.getProcessInstanceId());
		
		
		announce.setUserId(execution.getVariable("applyUserId").toString());
		announce.setTitle((String) execution.getVariable("title"));
		announce.setContent((String) execution.getVariable("content"));
		announce.setApplyTime(new Date());
		
		entityManager.persist(announce);
		return announce;
	}
	
	public void save(AnnounceJpaEntity announce){
		entityManager.persist(announce);
	}
	
	public AnnounceJpaEntity getLeave(Long id){
		return entityManager.find(AnnounceJpaEntity.class, id);
	} 
}
